$(function() {
  $(".navbar-expand-toggle").click(function() {
    $(".app-container").toggleClass("expanded");
    return $(".navbar-expand-toggle").toggleClass("fa-rotate-90");
  });
  return $(".navbar-right-expand-toggle").click(function() {
    $(".navbar-right").toggleClass("expanded");
    return $(".navbar-right-expand-toggle").toggleClass("fa-rotate-90");
  });
});

$(function() {
  return $('select').select2();
});

$(function() {
  $(".form_date").datetimepicker({
      language:  'zh-CN',
      format: "yyyy-mm-dd 00:00:00",
      minView: 'month',
      autoclose: true,
      todayBtn: true,
      pickerPosition: "bottom-left"
  });
});

// $(function() {
//   return $('.toggle-checkbox').bootstrapSwitch({
//     size: "small"
//   });
// });

// $(function() {
//   return $('.match-height').matchHeight();
// });

$(function() {
  return $(".side-menu .nav .dropdown").on('show.bs.collapse', function() {
    return $(".side-menu .nav .dropdown .collapse").collapse('hide');
  });
});

function loadForm($form, data){
    for(var name in data){
        var $ele = $form.find('*[name="' + name + '"]');
        if($ele.length <= 0)
          continue;

        var tagName = $ele[0].tagName.toLowerCase();

        switch(tagName){
            case "input":
                var type = $ele.attr("type");
                switch(type){
                    case "text":
                    case "password":
                    case "radio":
                    case "datetime":
                        $ele.val(data[name]);
                        break;
                    case "checkbox":
                        if(true === data[name])
                            $ele.attr("checked","true");
                        break;
                    case "file":
                        var inputFileType = $ele.attr("data-inputFileType");
                        if("image" === inputFileType){
                            $ele.attr("value", data[name]);
                            initInputFile($ele, "image");
                        }else if("file" === inputFileType){
                            $ele.attr("value", data[name]);
                            initInputFile($ele, "object");
                        }
                }
                break;
            case "select":
                $ele.val(data[name]);
                break;
            case "textarea":
                $ele.val(data[name]);
                break;
            case "div":
                var data_ckeditor = $ele.attr("data-ckeditor");
                if("true" == data_ckeditor){
                    $ele.html($(data[name]));
                    CKEDITOR.replace($ele.attr("id"));
                }
                break;
        }
    }
}

function initForm(){
    $('input[type="file"]').each(function(){
        var inputFileType = $(this).attr("data-inputFileType");
        if("image" === inputFileType){
            initInputFile($(this), "image");
        }else if("file" === inputFileType){
            initInputFile($(this), "object");
        }
    });
    $('div[data-ckeditor="true"]').each(function(){
        CKEDITOR.replace($(this).attr("id"));
    });
}

function getFormData($form){
    var params = {};
    $.each($form.serializeArray(), function(i, data) {
        if($form.find('*[name="' + data.name + '"]').attr("type") == "file"){
            params[data.name] = $form.find('*[name="' + data.name + '"]').data("url");
        }else if($form.find('*[name="' + data.name + '"]').attr("type") == "checkbox"){
            params[data.name] = $form.find('*[name="' + data.name + '"]').is(':checked');
        }else{
            params[data.name] =  data.value;
        }
    });
    $form.find('input[type="file"]').each(function(){
        params[$(this).attr("name")] = $(this).data("url").join(",");
    });
    $form.find('div[data-ckeditor="true"]').each(function(){
        params[$(this).attr("name")] =  CKEDITOR.instances[$(this).attr("name")].getData();
    });
    return params;
}

function initInputFile($input, allowedFileTypes){
    var initialPreview = [];
    var initialPreviewConfig = [];
    var urls = $input.attr("value");
    if(urls){
        $input.data("url", urls.split(","));
        $.each(urls.split(","),function(i, url){
            initialPreview.push(url);
            initialPreviewConfig.push({key:url})
        });
    }else{
        $input.data("url", []);
    }

    var overwriteInitial = true;
    var is_multiple = false;

    if($input.attr("multiple")){
        overwriteInitial = false;
        is_multiple = true;
    }

    $input.fileinput({
        language: 'zh',
        uploadUrl : "/fileInputUpload",
        deleteUrl : "/delete",
        showUpload: true,
        showRemove: true,
        allowedPreviewTypes: ['image'],
        allowedFileTypes: [allowedFileTypes],
        // allowedFileExtensions:  ['jpg', 'png'],
        maxFileSize : 2000,
        initialPreview :initialPreview,
        initialPreviewConfig : initialPreviewConfig,
        initialPreviewAsData : true,
        overwriteInitial : overwriteInitial
//            defaultPreviewContent: '<img src="/upload/default_avatar_male.jpg" alt="Your Avatar">',
    }).on("filebatchselected", function(event, files) {
        //$(this).fileinput("upload");
    }).on('fileuploaded', function(event, data, previewId, index) {
        var form = data.form, files = data.files, extra = data.extra,
            response = data.response, reader = data.reader;
        if(is_multiple){
            $(this).data("url").push(response.url[0]);
        }else{
            $(this).data("url", [response.url[0]])
        }
    }).on('filedeleted', function(event, key, jqXHR, data) {
        console.log('Key = ' + key);
        var url = $(this).data("url");
        url.splice($.inArray(key,url),1);
        $(this).data("url", url);
    }).on('fileimagesloaded', function(event) {
        console.log("fileimagesloaded");
    });
}
